package com.powerunion.datacollection.service;

import java.util.Date;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.powerunion.datacollection.common.Format;
import com.powerunion.datacollection.common.SystemException;
import com.powerunion.datacollection.dao.CollectionTaskDao;
import com.powerunion.datacollection.dao.DaoException;
import com.powerunion.datacollection.dao.DaoFactory;
import com.powerunion.datacollection.model.CollectionTask;
import com.powerunion.datacollection.model.jsonObject.CollectionTaskJSONObject;
import com.powerunion.util.log.LogHelper;

public class CollectionTaskManager {
	private static Logger logger = LogHelper.getInstance().getLogger(CollectionTaskManager.class);
	private CollectionTaskDao dao = null;
	public CollectionTaskManager() {
		super();
		this.dao = (CollectionTaskDao)DaoFactory.getInstance().getDao(CollectionTaskDao.class);
	}
	
	public String save(String saveStr, String currentUser)  throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(saveStr)) {
			String[] args = { "saveStr" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter saveStr NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}

		JSONObject jsonObject = JSONObject.fromObject(saveStr);
		CollectionTaskJSONObject ctjo = (CollectionTaskJSONObject) JSONObject.toBean(
				jsonObject, CollectionTaskJSONObject.class);
		CollectionTask ck = new CollectionTask();

		//复制属性值
	    BeanUtils.copyProperties(ck,ctjo);
	    
	    ck.setCreateDate(new Date());
	    
	    String startupDateStr = Format.getInstance().formatTime(ctjo.getStartupDateStr());
	    Date startupDate = Format.getInstance().stringToDate(startupDateStr, "yyyy-MM-dd");
	    ck.setStartupDate(startupDate);
	    
	    String completedDateStr = Format.getInstance().formatTime(ctjo.getCompletedDateStr());
	    Date completedDate = Format.getInstance().stringToDate(completedDateStr, "yyyy-MM-dd");
	    ck.setCompletedDate(completedDate);
	    
	    ck.setCrtUserCode(currentUser);
	    	     
		return this.dao.save(ck);

	}
	
	public boolean delete(String[] codes) {
		//参数为空
		if(codes.length < 1) {
			String[] args = { "codes" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter codes NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		return this.dao.delete(codes);
	}
	
	public CollectionTask findByKey(String id){
		//参数为空
		if(StringUtils.isEmpty(id)) {
			String[] args = { "id" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter id NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		return this.dao.getBykey(id);
	}
	
	public String getCollectionTasks() {
		List cts = this.dao.getCollectionTasks();
		JSONArray jsonArr = JSONArray.fromObject(cts);
		String result = "{results:"+jsonArr+"}";
		return result;
	}
	
	public boolean updateTaskInstanceNum(String id) throws DaoException {
		//参数为空
		if(StringUtils.isEmpty(id)) {
			String[] args = { "id" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter id NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.updateTaskInstanceNum(id);
	}
}
